/**
 * 购物车路由
 */

const express = require('express');
const router = express.Router();

const { networkStatus } = require('../utils/constant');
const { addShopCart, readShopCart, getShopCartDetail } = require('../file/shopCart');


/**
 * 添加和编辑购物车
 * @param {*} data 
 * @returns 
 */
router.post('/update', async (req, res) => {
    console.log('body', req.body);
    try {
        await addShopCart(req.body);
        res.send({
            code: 200,
            data: null,
            msg: networkStatus['200'],
        })
    }catch (error) {
        console.log(error);
        res.send({
            code: 500,
            data: null,
            msg: error ? error : networkStatus['500'],
        })
    }
})

/**
 * 查询所有购物车
 * @param {*} data 
 * @returns 
 */
router.post('/findAll', async (req, res) => {
    console.log('body', req.body);
    try {
        const data =  await readShopCart(req.body);
        res.send({
            code: 200,
            data,
            msg: networkStatus['200'],
        })
    }catch (error) {
        console.log(error);
        res.send({
            code: 500,
            data: null,
            msg: error ? error : networkStatus['500'],
        })
    }
})


// 根据id查询购物车
router.post('/getDetail', async (req, res) => {
    try {
        const data = await getShopCartDetail(req.body.userId);
        res.send({
            code: 200,
            data,
            msg: networkStatus['200'],
        })
    } catch (error) {
        res.send({
            code: 500,
            data: null,
            msg: error ? error : networkStatus['500'],
        })
    }
})

module.exports = router;
